import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/home',
    name: 'home',
    component: () => import('@/views/Home.vue'),
    children: [{
        path: 'welcom',
        name: 'welcom',
        component: () => import('@/components/Welcom.vue')
      },
      // 用户管理
      {
        path: 'users',
        name: 'users',
        component: () => import('@/components/Users.vue')
      },
      // 角色列表
      {
        path: 'roles',
        name: 'roles',
        component: () => import('@/components/Roles.vue')
      },
      // 权限列表
      {
        path: 'rights',
        name: 'rights',
        component: () => import('@/components/Rights.vue')
      },
      // 商品列表
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/components/Goods.vue'),
      },
      // 添加商品
      {
        path: 'add',
        name: 'add',
        component: () => import('@/components/Add.vue')
      },
      // 商品分类
      {
        path: 'categories',
        name: 'categories',
        component: () => import('@/components/Categories.vue')
      },
      // 分类参数
      {
        path: 'params',
        name: 'params',
        component: () => import('@/components/Params.vue')
      },
      // 订单列表
      {
        path: 'orders',
        name: 'orders',
        component: () => import('@/components/Orders.vue')
      },
      // 数据报表
      {
        path: 'reports',
        name: 'reports',
        component: () => import('@/components/Reports.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: "/",
    redirect: "/home/welcom"
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to, from, next) => {
  if (to.path === '/login')
    return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr)
    return next('/login');
  next();
})


export default router